package com.beilang.hotline.template.dao;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import com.beilang.base.core.dao.PaginationBaseDAO;
import com.beilang.base.pagination.PaginationHelper;
import com.beilang.base.util.StringUtil;
import com.beilang.hotline.template.model.TemplateModel;
import com.beilang.hotline.template.to.SearchTO;

@SuppressWarnings("unchecked")
public class TemplateDAO extends PaginationBaseDAO {
	public TemplateModel createTemplate(TemplateModel templateModel) {
        templateModel.setDeleteInd("N");
        return (TemplateModel)this.save(templateModel);
	}

	public TemplateModel saveTemplate(TemplateModel templateModel) {
		return (TemplateModel)this.save(templateModel);
	}

	public void deleteTemplate(Long templateId){
		TemplateModel template= this.getTemplate(templateId);
        template.setDeleteInd("Y");
        this.save(template);
	}

	public TemplateModel getTemplate(Long templateId){
		return (TemplateModel)this.get(TemplateModel.class, templateId);
	}

	public Integer searchCount(SearchTO searchTO) {
        Map map = new HashMap();
        if(searchTO.getSysId()!=null){
        	map.put("sysId", searchTO.getSysId());
        }
        String templateName=StringUtil.convertEmpty2Null(searchTO.getTemplateName());
        if(templateName!=null)templateName="%"+templateName+"%";
        map.put("templateName", templateName);
        String title=StringUtil.convertEmpty2Null(searchTO.getTitle());
        if(title!=null)title="%"+title+"%";
        map.put("title", title);
        return (Integer) getSqlMapClientTemplate().queryForObject("searchTemplateCount", map);
    }

    public List searchList(PaginationHelper helper, SearchTO searchTO) {
        Map map = new HashMap();
        if(searchTO.getSysId()!=null){
        	map.put("sysId", searchTO.getSysId());
        }
        String templateName=StringUtil.convertEmpty2Null(searchTO.getTemplateName());
        if(templateName!=null)templateName="%"+templateName+"%";
        map.put("templateName", templateName);
        String title=StringUtil.convertEmpty2Null(searchTO.getTitle());
        if(title!=null)title="%"+title+"%";
        map.put("title", title);        
        map = constructPaginationMap(helper, map);
        return getSqlMapClientTemplate().queryForList("searchTemplateList", map);
    }

     public List searchList(SearchTO searchTO) {
        Map map = new HashMap();
        if(searchTO.getSysId()!=null){
        	map.put("sysId", searchTO.getSysId());
        }
        String templateName=StringUtil.convertEmpty2Null(searchTO.getTemplateName());
        if(templateName!=null)templateName="%"+templateName+"%";
        map.put("templateName", templateName);
        String title=StringUtil.convertEmpty2Null(searchTO.getTitle());
        if(title!=null)title="%"+title+"%";
        map.put("title", title);
        return getSqlMapClientTemplate().queryForList("searchTemplateAllList", map);
    }

    protected  String mapSortField(String dataField){
    	if(StringUtil.isNullOrBlank(dataField))return dataField;
    	if(dataField.equalsIgnoreCase("sysName")) return "system_name";
    	if(dataField.equalsIgnoreCase("templateName")) return "template_name";
    	if(dataField.equalsIgnoreCase("title")) return "title";        
        return dataField;
    }

    public void deleteAllSysTemplates(Long sysId){
        Map map = new HashMap();
        map.put("sysId", sysId);
        getSqlMapClientTemplate().update("deleteAllSysTemplates",map);
    }

}
